#include"../image/image_util.h"
#include"texture.h"
Texture::Texture(const Texture&t) : filename(t.filename),val(t.val) {
	if (t.filename != "") {
		w = t.w;
		h = t.h;
		c = t.c;
		feature_map = new Spectrum[w * h];
		memcpy(feature_map, t.feature_map, w * h * sizeof(Spectrum));
	}
	else
		feature_map = NULL;
}
Texture::Texture(std::string name, bool gamma_correction) : filename(name) {
	if (name != "") {
		feature_map = loadImage(filename, &w, &h, &c, gamma_correction);
		val = Spectrum(0.);
	}
	else
		feature_map = NULL;
}

Texture::Texture(const Spectrum& val) :val(val), filename(""), feature_map(NULL) {
}

Spectrum Texture::_get_color_at(float x, float y, SpectrumType type) const
{
	if (!feature_map) return val;
	int pw = (int(x * w) % w + w) % w, ph = (int(y * h) % h + h) % h;
	int i = ph * w + pw;

	return feature_map[i];
}
